///--------------------------------------------------
/**
 *
 *      Hand Gesture Recognition System
 *
 */
//--------------------------------------------------
/**
 * //PREPROCESSING CLASS//
 * Preprocesa los frames
 *
 * @author  Edwin Cobos & Cesar Murcia & Wilson Sarmiento
 * @version HGR_v1.5
 *
 * Revisado: 22/02/12
 */
//--------------------------------------------------
package HGR;
//IMPORT//
//--------------------------------------------------

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *Preprocesa los frames, Heredada de la clase Thread de Java
 */
public class PreProcessing extends Thread
{
    //--------------------------------------------------
    private Frame       Actual_Frame;
    private float[]     BackGround;
    private float       AvgBackGround;
    private float       VarBackGround;
    private float[]     buffer;
    private float[]     labels;
    private float[]     img_label;
    private Frame       BGS_Frame;
    private CamDisplay  show;
    private boolean     Alive;
    private DescriptorPro  ThreadsDescS;
    //--------------------------------------------------
    /**
     * Constructor parametrico
     * @param A_Frame
     */
    public PreProcessing(Frame A_Frame, float[] BkGround, float AvgBkGround, float VarBkGround)
    {
        Actual_Frame = new Frame(A_Frame.getBuffer(), A_Frame.getIndice(), A_Frame.getWidth(), A_Frame.getHeight());
        BackGround = new float[BkGround.length];
        BackGround = BkGround;
        AvgBackGround = AvgBkGround;
        VarBackGround = VarBkGround;
        buffer = new float[Actual_Frame.getBuffer().length];
        img_label = new float[Actual_Frame.getBuffer().length];
        Alive = true;
    }
    public PreProcessing()
    {
    }
    public void asd(){}
    //--------------------------------------------------
    /**
     * Ejecuta el proceso
     */
    @Override
    public void run()
    {
        buffer = Core.LowPassFilter(Actual_Frame.getBuffer(), Core.lp_kernel, Actual_Frame.getWidth(), Actual_Frame.getHeight());
        buffer = Core.MedianFilter(buffer, 3, Actual_Frame.getWidth(), Actual_Frame.getHeight());
        buffer = Core.BackgroundSubtraction(buffer, BackGround, AvgBackGround, VarBackGround, HGR_gui.GetThresholdBgS());
        buffer = Core.DilatationFilter(Core.kernel5_Circle, buffer, Actual_Frame.getWidth(), Actual_Frame.getHeight());
        buffer = Core.ErosionFilter(Core.kernel5_Circle, buffer,Actual_Frame.getWidth(), Actual_Frame.getHeight());

        labels = Core.LabeledRegions(img_label, buffer, Actual_Frame.getWidth(), Actual_Frame.getHeight()); //Calcula las regiones en labels[] y las etiquetas en img_label[]
        Core.RemoveRegions(img_label, labels, Actual_Frame.getWidth(), Actual_Frame.getHeight()); //Remueve las regiones dejando únicamente la más grande
        img_label = Core.DilatationFilter(Core.kernel5_Circle, img_label, Actual_Frame.getWidth(), Actual_Frame.getHeight());
        img_label = Core.MaskImage(img_label, Actual_Frame.getBuffer()); //Aplica una máscara de img_label a bufferOrigin[]
        
        BGS_Frame = new Frame(img_label, Actual_Frame.getIndice(), Actual_Frame.getWidth(), Actual_Frame.getHeight());

        if(HGR_gui.VisCheck.isSelected()){
            show = new CamDisplay(BGS_Frame, HGR_gui.BgsFrame);
            show.start();
        }
        /*try {
            show.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(PreProcessing.class.getName()).log(Level.SEVERE, null, ex);
        }*/

        
        ThreadsDescS = new DescriptorPro(BGS_Frame);        
        ThreadsDescS.start();
        try {
            
            ThreadsDescS.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(PreProcessing.class.getName()).log(Level.SEVERE, null, ex);
        }

         Alive = false;
    }
        //--------------------------------------------------
}
//--------------------------------------------------
//FIN
//--------------------------------------------------